package com.wjw.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjw.pojo.Book;
import com.wjw.pojo.Reader;
import com.wjw.pojo.ReaderBook;
import com.wjw.service.BookSersive;
import com.wjw.service.ReaderBookService;
import com.wjw.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("normalUser")
public class NormalUserController {

    @Autowired
    private BookSersive bookService;

    @Autowired
    private ReaderBookService readerBookService;

    @Autowired
    private ReaderService readerService;

    //我的图书
    @RequestMapping("myBookList")
    public String myBookList(HttpSession session,
                             @RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
                             @RequestParam(required = false,defaultValue = "10",value = "pageSize") Integer pageSize, Model model){
        if (pageNum <= 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageSize <= 0 || pageSize.equals("") || pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        Integer userId =(Integer) session.getAttribute("userId");
        PageInfo<Book> pageInfo= readerBookService.myBookList(userId);
        model.addAttribute("pageInfo", pageInfo);
        return "myBookList";
    }

    //普通用户还书
    @RequestMapping("returnBook/{id}")
    public String returnBook(@PathVariable("id")Integer id, HttpSession session){
        Integer userId =(Integer) session.getAttribute("userId");
        QueryWrapper<ReaderBook> qw=new QueryWrapper<>();
        qw.eq("rid",userId);
        qw.eq("bid",id);
        ReaderBook readerBook = new ReaderBook();
        readerBook.setStatus(1);
        boolean update = readerBookService.update(readerBook, qw);
        //还完书后库存加一
        Book book = bookService.getById(id);
        book.setStock(book.getStock()+1);
        bookService.updateById(book);
        return "redirect:/normalUser/myBookList";
    }

    //展示图书分页,进行模糊查询
    @RequestMapping("listBook")
    public String list(@RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
                       @RequestParam(required = false,defaultValue = "12",value = "pageSize") Integer pageSize, Model model, Book book) {
        if (pageNum <= 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageSize <= 0 || pageSize.equals("") || pageSize == null) {
            pageSize = 12;
        }
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<Book> qw=new QueryWrapper<>();
        if (book.getBname()!=null){
            qw.like("bname",book.getBname());
        }
        if (book.getType()!=null){
            qw.like("type",book.getType());
        }
        List<Book> list = bookService.list(qw);
        PageInfo<Book> objectPageInfo = new PageInfo<Book>(list);
        model.addAttribute("pageInfo", objectPageInfo);
        return "normalUserBookList";
    }

    //借阅图书
    @RequestMapping("borrowBook/{id}")
    public String borrowBook(HttpServletRequest request, @PathVariable("id")Integer id){
        Integer userId =(Integer) request.getSession().getAttribute("userId");
        ReaderBook readerBook = new ReaderBook();
        readerBook.setBid(id);
        readerBook.setRid(userId);
        boolean save = readerBookService.save(readerBook);
        //借阅完后库存减一
        Book book = bookService.getById(id);
        book.setStock(book.getStock()-1);
        bookService.updateById(book);
        return "redirect:/normalUser/myBookList";
    }

    //推荐图书
    @RequestMapping("recommendBookList")
    public String returnBook(HttpServletRequest request,
                             @RequestParam(required = false,defaultValue = "1",value = "pageNum") Integer pageNum,
                             @RequestParam(required = false,defaultValue = "10",value = "pageSize") Integer pageSize, Model model, Book book){
        Integer userId =(Integer) request.getSession().getAttribute("userId");
        QueryWrapper<Reader>qw=new QueryWrapper<>();
        qw.eq("id",userId);
        Reader reader = readerService.getOne(qw);
        String type = reader.getType();
        String[] split = StrUtil.split(type, ",");
        List<String>typeList=new ArrayList<>();
        for (String s : split) {
            if (!s.isEmpty()){
                typeList.add(s);
            }
        }
        QueryWrapper<Book>qw1=new QueryWrapper<>();
        if (book.getBname()!=null){
            qw1.eq("bname",book.getBname());
        }
        qw1.in("type",typeList);
        List<Book> list = bookService.list(qw1);
        PageInfo<Book>pageInfo=new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        return "recommendBookList";
    }

}
